<?php
// Module Body

function ext_personal_userinfo(&$data) {
	global $personaldata;

	rpc_init_vars($data['data']);

	$data['result'] = rpc_data(fetchdata("username,password,id,name,level,type,shell,root,hd,hd_used,aliases,catchall,autoreply,forward,favorites,lang,skin,suspend,ip_restrict","user", array($personaldata['username'], $personaldata['id'])));
}

function ext_personal_userset(&$in) {
	global $personaldata, $domaindata, $cfg;

	if(!rpc_init_vars($in['data'])) {
		$in['result'] = rpc_error(4099, 'Permission denied');
		return;
	}

	$data = $in['data'];

	// Establish domain prefix
	list($domainprefix) = explode('.',$domaindata['domain']);

	// HARDCODED $data['uid'], 'id'
	$data['uid'] = $personaldata['username'];
	$data['id'] = $personaldata['id'];

	if ($cfg['prepend_domain_to_username']) {
		// HARDCODED $data['username'] to include domainprefix
		$data['username'] = $domainprefix."-".$data['username'];
	}
	//We cant't change access level ourselves if restrict_access_level_change enabled
	if (($cfg['restrict_access_level_change']) && ($userdata['username'] == $personaldata['username'])) {
		$data['level'] = $personaldata['level'];
	}

    if (($userdata['level'] >= USER_LEVEL_USER) && ($personaldata['catchall']=="false")) {
		$data['catchall'] = false;
	}

	//call_hook('web:personal:userset:submit', $data);

	$error = user("update", $data);
	if(empty($error))
		$in['result'] = rpc_data(fetchdata("username,id","user", array($data['username'], $data['id'])));
	else
		$in['result'] = rpc_error(4100, implode("\n", $error));
}

function ext_domain_newuser(&$in) {
	global $personaldata, $domaindata, $cfg;

	//var_dump($in['data']);

	//only the domainid needs to match
	if(!rpc_init_vars($in['data']) && $domaindata['id']!=$in['data']['id']) {
		//var_dump($domaindata['id'], $in['data']['id']);
		$in['result'] = rpc_error(4099, 'Permission denied');
		return;
	}

	$data = $in['data'];

	if(isset($data['host']) && !isset($data['hostname']))
		$data['hostname'] = $data['host'];


	// Establish domain prefix
	list($domainprefix) = explode('.',$domaindata['domain']);

	// HARDCODED $data['id']
	$data['id'] = $domaindata['id'];

	if ($cfg['prepend_domain_to_username']) {
		// HARDCODED $data['username'] with domainprefix-
		$usernametmp = $data['username'];
		$data['username'] = $domainprefix . "-" . $usernametmp;
	}

	//var_dump($data);

	$error = user("create", $data);
	if(empty($error))
		$in['result'] = rpc_data(fetchdata("username,id","user", array($data['username'], $data['id'])));
	else
		$in['result'] = rpc_error(4100, implode("\n", $error));
}


function ext_domain_domaininfo(&$data) {
	global $domaindata, $personaldata;

	rpc_init_vars($data['data']);

	if(!verify_access_level($personaldata['level'], USER_LEVEL_DOMAIN)) {
		$data['result'] = rpc_error(4099, 'Permission denied');
		return;
	}

	$ret = fetchdata("id,owner,ip,host,domain,aliases,catchall,nohost,redirect,path,defaultroot,type,transfer,hd,users,subdomains,pointers,bandwidth,hd_used,transfer_used,serverside,db,shell,email,suspended", "domain", $domaindata['id']);
	$data['result'] = rpc_data($ret);
}

function ext_reseller_newdomain(&$in) {
	global $personaldata, $domaindata, $resellerdata, $cfg;

	if(!rpc_init_vars($in['data'])
	   || !verify_access_level($personaldata['level'], USER_LEVEL_RESELLER)) {
		$in['result'] = rpc_error(4099, 'Permission denied');
		return;
	}

	$data = $in['data'];

	//for some reason, domain() uses different names from fetchdata()
	//convert them here (this is useful in case you use system.cascadecall
	//$convert = array('host' => 'hostname', 'ip' => 'ip_addr');
	if(isset($data['host']) && !isset($data['hostname']))
		$data['hostname'] = $data['host'];
	if(isset($data['ip']) && !isset($data['ip_addr']))
		$data['ip_addr'] = $data['ip'];

	// HARDCODED $data['id'], $data['type'];
	$data['id'] = $resellerdata['id'];
	$data['type'] = 'domain';


	/* This should probably be in mod_cband (??)
	if ($cfg['bandwidth'] && $data['bandwidth'] != 0 && $in['resellerdata']['bandwidth'] != 0 && $data['bandwidth'] > $in['resellerdata']['bandwidth']) {
		$data['bandwidth'] = $resellerdata['bandwidth'];
	}*/

	$error = domain("create",$data,$return);
	if(empty($error)) {
		$ret=array('id' => $return);
		$in['result'] = rpc_data($ret);
	} else {
		$in['result'] = rpc_error(4100, implode("\n", $error));
	}
}

function ext_listmethods(&$in) {
	global $hooks;
	$methods = array('system.login', 'system.multicall', 'system.cascadecall', 'system.listMethods');
	foreach($hooks as $k=>$v) {
		if(substr($k, 0, 4)=='ext:') {
			$methods[] = str_replace(':', '.', substr($k, 4));
		}
	}
	sort($methods);
	$in['result'] = rpc_data($methods);
}

function ext_gettime(&$in) {
	$in['result'] = rpc_data(XMLRPC_convert_timestamp_to_iso8601(gmtime()));
}

function ext_wait(&$in) {
	if(isset($in['data']['id'])) {
		if(isset($in['data']['username'])) {
			//wait for username to be initialized (created by backend)
			$data = fetchdata('action', 'user', array($in['data']['username'], $in['data']['id']));
			//check if the user actually exists
			if($data!=null) {
				while (!empty($data['action'])) {
					//this makes sure we don't run out of memory
					unset($data);
					sleep(1);
					//usleep(100);
					$data = fetchdata('action', 'user', array($in['data']['username'], $in['data']['id']));
				}
				unset($data);
			}
		} elseif (strlen($in['data']['id'])==5){
			//reseller, there is nothing to wait for...
		} else {
			//domain
			$data = fetchdata('action', 'domain', $in['data']['id']);
			if($data!=null) {
				while (!empty($data['action'])) {
					//this makes sure we don't run out of memory
					unset($data);
					sleep(1);
					//usleep(100);
					$data = fetchdata('action', 'domain', $in['data']['id']);
				}
				unset($data);
			}
		}
	} else
		sleep(2); //just wait a little

	//passhrough
	$in['result'] = rpc_data($in['data']);
}

?>
